﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MyMedia.FFmpeg;
using System.Threading;

namespace MyMedia_App.Pages
{
    public partial class FFmpeg_Tab : Form
    {
        Thread ConvertThread;
        public FFmpeg_Tab()
        {
            InitializeComponent();
        }
        private void FFmpeg_Tab_Load(object sender, EventArgs e)
        {
            if (!File.Exists(Application.StartupPath + "\\ffmpeg.exe"))
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                MessageBox.Show("Video2Audio Disabled FFmpeg not found!");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "MP3|*.mp3";
            saveFileDialog1.Title = "Save Audio";
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.FileName != "")
            {
                textBox2.Text = saveFileDialog1.FileName;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            ConvertThread = new Thread(new ThreadStart(Convert2Audio));
            ConvertThread.Start();
            if (ConvertThread.IsAlive)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                label3.Text = "Converting Video...";
            }
            groupBox2.Visible = true;
        }

        private void Convert2Audio()
        {
            MyMedia.FFmpeg.Encoder enc = new MyMedia.FFmpeg.Encoder();
            enc.FFmpegPath = Application.StartupPath + "\\ffmpeg.exe";
            VideoFile input = new VideoFile(textBox1.Text);
            EncodedVideo encoded = enc.EncodeVideo(input, QuickAudioEncodingCommands.MP3128Kbps, textBox2.Text, false);
            this.EndInvoke(this.BeginInvoke(new MethodInvoker(delegate
            {
                if (encoded.Success)
                {
                    button1.Enabled = true;
                    button2.Enabled = true;
                    button3.Enabled = true;
                    label3.Text = "Complete..";
                    if (ConvertThread.IsAlive)
                    {
                        ConvertThread.Abort();
                    }
                }
                else
                {
                    button1.Enabled = true;
                    button2.Enabled = true;
                    button3.Enabled = true;
                    label3.Text = "Error!";
                    if (ConvertThread.IsAlive)
                    {
                        ConvertThread.Abort();
                    }
                }
            })));
        }
    }
}
